package com.syj.jsxjj.ui.lend.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.syj.jsxjj.R;
import com.syj.jsxjj.ui.authentication.activity.ShowSinglePictureActivity;
import com.syj.jsxjj.ui.lend.bean.HomeBean;
import com.syj.jsxjj.ui.lend.bean.HomeIndexResponseBean;

import java.util.ArrayList;
import java.util.List;


public class HomeBannerImageAdapter extends PagerAdapter {
    private HomeIndexResponseBean.IndexImagesBean bannerInfo;
    private ArrayList<ImageView> viewlist;
    private List<HomeBean.BannerListBean> indexBannerInfos = new ArrayList<>();
    private Context context;

    public HomeBannerImageAdapter(Context context, List<HomeBean.BannerListBean> indexBannerInfos) {
        viewlist = new ArrayList<>();
        this.context = context;
        this.indexBannerInfos = indexBannerInfos;
        for (int index = 0; index < indexBannerInfos.size(); index++) {
            viewlist.add(getImageView(index));
        }
//		if (indexBannerInfos.size() == 2) {
//			viewlist.add(getImageView(0));
//			viewlist.add(getImageView(1));
//		}
    }

    private ImageView getImageView(final int index) {

        ImageView imageView = new ImageView(context);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        imageView.setLayoutParams(layoutParams);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        Glide.with(context).load(indexBannerInfos.get(index).getImgUrlRes())
                .placeholder(R.mipmap.banner)
                .error(R.mipmap.banner)
                .into(imageView); //设置图片
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ShowSinglePictureActivity.class);
//				intent.putExtra("title", indexBannerInfos.get(index).getTitle());
                intent.putExtra("urlRes", indexBannerInfos.get(index).getLinkImg());
                context.startActivity(intent);

            }
        });
        return imageView;
    }

    @Override
    public int getCount() {
        return indexBannerInfos.size() == 1 ? 1 : Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // super.destroyItem(container, position, object);
        // container.removeView(viewlist.get(position));
    }

    public int getRealCount() {
        return indexBannerInfos != null ? indexBannerInfos.size() : 0;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView view = null;
        try {
            position %= indexBannerInfos.size() == 2 ? indexBannerInfos.size() * 2 : indexBannerInfos.size();
            view = viewlist.get(position);
            ViewParent vp = view.getParent();
            if (vp != null) {
                ViewGroup parent = (ViewGroup) vp;
                parent.removeView(view);
            }
            container.addView(view);
        } catch (Exception e) {
            // TODO: handle exception
        }

        return view;
    }

    public void updateItem(List<HomeBean.BannerListBean> indexBannerInfos, ViewPager container) {
        this.indexBannerInfos = indexBannerInfos;
        container.removeAllViews();
        viewlist.clear();

        for (int index = 0; index < indexBannerInfos.size(); index++) {
            viewlist.add(getImageView(index));
        }
        if (indexBannerInfos.size() == 2) {
            viewlist.add(getImageView(0));
            viewlist.add(getImageView(1));
        }
        container.setAdapter(this);
        container.requestLayout();
    }

}
